﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace FrameWork.DataBaseProvider
{
    public class OperateSql
    {
        private DbHelper db;
        public OperateSql()
        {
            db = new DbHelper();
        }

        public static OperateSql Instance
        {
            get
            {
                return new OperateSql();
            }
        }

        public object GetObjectSelect(string tableName, string field, string filter)
        {
            string Sql = SimDataSql(tableName, field, filter);
            return db.GetObject(Sql);
        }

        /// <summary>
        /// 返回带分页的数据集
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pageSize"></param>
        /// <param name="currPageIndex"></param>
        /// <param name="dataSetName"></param>
        /// <returns></returns>
        public DataSet GetPagerArray(string sql, int pageSize, int currPageIndex, string dataSetName)
        {
            return db.GetPagerDataSet(CommandType.Text, sql, pageSize, currPageIndex, dataSetName);
        }

        /// <summary>
        /// 返回查询语句
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="field"></param>
        /// <param name="filter"></param> 
        /// <returns></returns>
        public string SimDataSql(string tableName, string field, string filter)
        {
            string Sql = string.Empty;
            if (string.IsNullOrEmpty(field))
            {
                if (string.IsNullOrEmpty(filter))
                {
                    Sql = string.Format("select * from {0} ", tableName);
                }
                else
                {
                    Sql = string.Format("select * from {0} where {1} ", tableName, filter);
                }
                
            }
            else
            {
                if (string.IsNullOrEmpty(filter))
                {
                    Sql = string.Format("select {0} from {1} ",field, tableName);
                }
                else
                {
                    Sql = string.Format("select {0} from {1} where {2} ",field, tableName, filter);
                }
            }

          //  Sql += string.Format(" order by {0} desc ",TableMapPrimaryArr.TableDic[tableName]);

            return Sql;
        }

        /// <summary>
        /// 构建并执行查询语句
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="field"></param>
        /// <param name="filter"></param>
        /// <returns></returns>
        public DataSet SimSqlDataEx(string tableName, string field, string filter)
        {
            string Sql = SimDataSql(tableName, field, filter);
            return db.CreateDataSet(CommandType.Text, Sql) ;
        }

        /// <summary>
        /// 构建插于语句并返回
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="field"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public string AddDataSql(string tableName, string field, string value)
        {
            string sql = "insert into " + tableName + "(" + field + ")values(" + value + ")";
            return sql;
        }

        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <param name="field">字段</param>
        /// <param name="value">值</param>
        /// <returns></returns>
        public string AddDataEx(string tableName, string field, string value)
        {
            string sql = AddDataSql(tableName, field, value);


            if (db.ExecuteNonQuery(CommandType.Text, sql) > 0)
            {
                return "添加成功";
            }
            else
            {
                return "添加失败";
            }
        }

        public string UpdateDataSql(string tableName, string set, string where)
        {
            string sql = "update " + tableName + " set " + set + " where " + where + "";
            return sql;
        }

        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <param name="set"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        public string UpdateDataEx(string tableName, string set, string where)
        {
            string sql = UpdateDataSql(tableName, set, where);
            if (db.ExecuteNonQuery(CommandType.Text, sql) > 0)
            {
                return "编辑成功";
            }
            else
            {
                return "编辑失败";
            }
        }

        public string StaticDeleteDataSql(string tableName, string set, string where)
        {
            return UpdateDataSql(tableName, set, where);
        }

        /// <summary>
        /// 状态删除数据Sql
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="set"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        public string StaticDeleteDataEx(string tableName, string set, string where)
        {
            if (UpdateDataEx(tableName, set, where) == "编辑成功")
            {
                return "删除成功";
            }
            else
            {
                return "删除失败";
            }
        }

        public string TrueDeleteDataSql(string tableName, string where)
        {
            string sql = "delete from " + tableName + "  where  " + where + "";
            return sql;
        }

        /// <summary>
        /// 物理删除数据Sql
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="where"></param>
        /// <returns></returns>
        public string TrueDeleteDataEx(string tableName, string where)
        {
            string sql = TrueDeleteDataSql(tableName, where);
            if (db.ExecuteNonQuery(CommandType.Text, sql) > 0)
            {
                return "删除成功";
            }
            else
            {
                return "删除失败";
            }
        }


    }
}
